// pages/car/car.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRedact: false,
    productList: [],
    chooseAll: false,
    priceTotal: 0
  },

  numChange() {
    let list = this.get()
    let priceTotal = 0
    for (let i in list) {
      if (list[i].ischoose) priceTotal += list[i].detail.price * list[i].num
    }
    this.setData({
      priceTotal: priceTotal
    })
  },

  spcartList(start) {
    wx.$https('spcart.list', {
      start: 0,
      limit: start + 10
    }).then(res => {
      this.setData({
        productList: res
      })
      if (res == '') {
        this.setData({
          chooseAll: false,
          priceTotal: 0
        })
        return
      }
      this.checkAll()
    })
  },

  get() {
    let productList = this.data.productList
    let list = []
    for (let i = 0; i < productList.length; i++) {
      for (let n = 0; n < productList[i].items.length; n++) {
        list.push(this.selectComponent(`#pro${productList[i].items[n].spcartItemsId}`)[`getIs`]())
      }
    }
    return list
  },

  checkAll() {
    let productList = this.data.productList
    let key = true
    for (let i = 0; i < productList.length; i++) {
      for (let n = 0; n < productList[i].items.length; n++) {
        if (this.selectComponent(`#pro${productList[i].items[n].spcartItemsId}`).data.is == false) {
          key = false
        }
      }
    }
    this.setData({
      chooseAll: key
    })
  },

  chooseAll() {
    let productList = this.data.productList
    let choose = this.data.chooseAll ? 'noChoose' : 'choose'
    for (let i = 0; i < productList.length; i++) {
      for (let n = 0; n < productList[i].items.length; n++) {
        this.selectComponent(`#pro${productList[i].items[n].spcartItemsId}`)[`${choose}`]()
      }
    }
    this.setData({
      chooseAll: !this.data.chooseAll
    })
  },

  childChange(e) {
    let list = this.get()
    let key = true
    for (let x of list) {
      if (!x.ischoose)
        key = false
    }
    this.setData({
      chooseAll: key
    })
  },

  redact() {
    this.setData({
      isRedact: !this.data.isRedact
    })
  },

  navOrder() {
    let list = this.get()
    let proList = JSON.parse(JSON.stringify(this.data.productList));
    for (let i in list) {
      if (list[i].ischoose) {
        for (let n in proList) {
          for (let m in proList[n].items) {
            if (proList[n].items[m].goodsSkuId == list[i].detail.goodsSkuId) {
              proList[n].items[m].num = list[i].num
            }
          }
        }
      }
    }
    for (let k = 0; k < proList.length; k++) {
      for (let y = 0; y < proList[k].items.length; y++) {
        if (!proList[k].items[y].num) {
          proList[k].items.splice(y, 1)
          y--
        }
      }
    }
    for (let o = 0; o < proList.length; o++) {
      if (proList[o].items.length === 0) {
        proList.splice(o, 1)
        o--
      }
    }
    if (proList.length == 0) {
      wx.showToast({
        title: '请至少选择一项商品',
        icon: 'none'
      })
      return
    }
    wx.$nav('/pages/orderM/orderM', {
      productList: JSON.stringify(proList)
    })
  },

  delete() {
    let list = this.get()
    let spcartItemsId = '',
      num = 0
    for (let i in list) {
      if (list[i].ischoose) {
        spcartItemsId += spcartItemsId ? `,${list[i].detail.spcartItemsId}` : list[i].detail.spcartItemsId
        num++
      }
    }
    this.setData({
      delIds: spcartItemsId
    })
    if (spcartItemsId) {
      this.delAsk.hideShow({
        title: '删除',
        info: `确认删除${num}项`
      })
    } else {
      wx.showToast({
        title: '请选择要删除的商品',
        icon: 'none'
      })
    }
  },

  delete_(e) {
    if (e.detail.type == 0) return
    let spcartItemsId = this.data.delIds
    wx.$https('spcart.items.del', {
      spcartItemsId: spcartItemsId
    }).then(res => {
      wx.showToast({
        title: '删除成功',
      })
      this.spcartList(0)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.$isLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.delAsk = this.selectComponent('#delAsk')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.spcartList(0);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.spcartList(this.data.productList.length);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})